<?php

namespace app\service\upload;


use app\common\exception\MiException;
use app\model\sys\UploadFile;
use think\facade\Db;
use think\facade\Filesystem;

class UploadService
{
    private string $filename = 'file'; // 上传文件名称
    private string $disk = 'public';
    private string $subPath = '/';


    public function setFilename(string $filename): static
    {
        $this->filename = $filename;
        return $this;
    }

    /**
     * @throws MiException
     */
    public function setDisk($disk): static
    {
        if (!$disk) $disk = request()->param('disk');
        if (!$disk) {
            throw new MiException("上传空间错误");
        }
        $this->disk = $disk;
        return $this;
    }

    /**
     * @throws MiException
     */
    public function setSubPath($subPath): static
    {
        if (!$subPath) {
            throw new MiException("上传子目录设置错误");
        }
        $this->subPath = $subPath;
        return $this;
    }


    // tp上传文件
    public function upload(int $site_id, int $uid=0): UploadFile|\think\Model
    {

        $file = request()->file($this->filename);

        //            validate([
//                'file'  => $file,
//            ],  [
//                'file' => $this->validateRule()
//            ]);

        $path = Filesystem::disk($this->disk)->putFile('uploads'.$this->subPath, $file);
        // windows系统中路径反斜杠处理
        $path = '/' . str_replace('\\', '/', $path);
        $pathArr = miExplode($path, "/");
        $arrCount = count($pathArr);
//            $name = substr($path,strripos($path,"/")+1);
        $name = $pathArr[$arrCount];
        $dir = $pathArr[$arrCount-1];
        // 上传驱动
//            $savename = $this->uploadDriver($savename);

        // 存入数据库
        $uploadFile = UploadFile::create([
            'site_id' => $site_id,
            'uid' => $uid,
            'save_name' =>$name,
            'original_name' => $file->getOriginalName(),
            'path' => $path,
            'file_size' => $file->getSize(),
            'file_ext' => $file->getOriginalExtension(),
            'file_mime' => $file->getOriginalMime(),
            'file_dir' => $dir,
            'save_type' => $this->disk,

        ]);

        return $uploadFile;
    }


    // 上传驱动
    protected function uploadDriver(string $fineName = '')
    {

        if ($fineName) {
            $uploadDriver = $this->system['upload_driver'];
            if ($uploadDriver == '1') {
                return $fineName;

            } else if ($uploadDriver == '2') {
                // 阿里云
                $urlArr = hook('aliyunOssHook', ['url' => $fineName]);
                $urlArr = json_decode($urlArr, true);
                if (isset($urlArr['error']) && $urlArr['error'] == 0) {
                    return $urlArr['data'];
                } else {
                    return $urlArr;
                }

            } else if ($uploadDriver == '3') {
                // 七牛云
                $urlArr = hook('qiniuOssHook', ['url' => $fineName]);
                $urlArr = json_decode($urlArr, true);
                if (isset($urlArr['error']) && $urlArr['error'] == 0) {
                    return $urlArr['data'];
                } else {
                    return $urlArr;
                }
            }
        }
    }

}
